package nextoffer;

/**
 * @author ：szq
 * @date ：Created in 2022/3/16 21:38
 * @description：剑指 Offer 22. 链表中倒数第k个节点
 * @modified By：
 * @version: $
 */
public class theKthToLastNodeInALinkedList {

    public class ListNode {
     int val;
     ListNode next;
     ListNode(int x) { val = x; }
  }
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode res = head;
        while(k!=0)
        {
            head = head.next;
            k--;
        }

        while(head!=null)
        {
            res = res.next;
            head = head.next;

        }
        return  res;
    }
}
